/**
 * @author Kristian Mandrup
 * @date June 10, 2009
 * @version 0.1
 * 
 * jsTree.generator :: eventController
 * 
 * - Central controller for event handling
 */

m3gen.eventList = [];

m3gen.log = function(message, params) {
	console.log(message);
	m3gen.eventList.push(message);	
}



m3gen.eventController = {
	eventHandlers: m3gen.eventHandlers,
	settings: {},
	handlerMap: {
		generator: ['onBeginGenerateTreeInstanceModel', 'OnNoTreeParseModelError', 'onCompleteGenerateTreeInstanceModel'],
		parser: ['onBeginParseInstance', 'onCompleteParseInstance', 'onBeginParseInstanceItem', 
				 'onCompleteParseInstanceItem', 'onBeginParseWithGeneratorRuleSet', 'onNoRuleSetForObjTypeWarning',
				 'onNoSchemaError', 'onMalformedSchemaError', 'onNoJsTreeNodeError', 'onInvalidJsTreeNodeWarning',
				 'onFoundNoBranchNode', 'onBeginSelector', 'onUseSelectorPath', 'onFoundObject', 'onCompleteSelector',
				 'onCallSelectorFunction', 'onNotFoundObject'],	
		branch: ['onBeginResolveChildren', 'onCompleteResolveChildren', 'onBeginCreateBranchNode', 
				 'onCompleteCreateBranchNode', 'onBeginResolveBranch', 'onCompleteResolveBranch',
				 'onBeginResolveBranches', 'onCompleteResolveBranches', 'onBeginResolveSimpleObject',
				 'onFoundJsonReference', 'onBeginResolveObject', 'onCompleteResolveObject', 'onCompleteResolveSimpleObject',
				 'onBeginResolveArrayObject', 'onCompleteResolveArrayObject', 'onMalformedBranchWarning',
				 'onFoundUnresolvedJsonReference', 'onResolveObjectError', 'onFoundObject',
				 'onNotFoundObject', 'onNoSelectorsFound', 'onBeginSelector', 'onCompleteSelector',
				 'onCallSelectorFunction', 'onUseSelectorPath', 'onBranchNodeAddedToChildren',
				 'onEmptyBranchNodeNotAdded', 'onEmptyArrayObjectFound', 'onResolveJsonReference', 'onTriedResolveJsonBefore'
		],
		setNode: ['onBeginSetNode', 'onCompleteSetNode'],
		id: ['onBeginBuildData', 'onBeginBuildAttributes', 'onBeginBuildState'],
		select: ['onBeginSelectId', 'onBeginSelectTitle', 'onBeginSelectIcon', 'onBeginSelectState']		
	}, 	
	handle: function(handlerName, event, context){
		/*
		var log = m3gen.generator.log;
		// loop through each key in handlerMap, use 
		var oc = m3gen.util.oc;
		var ev = m3gen.util.event;
		var evh = this.eventHandlers;
		// loop through handlerMap
		for (var handlerMapKey in this.handlerMap) {
			// if handlerName is found in array of handlerNames for the key
			var handlerList = this.handlerMap[handlerMapKey];
			if (handlerName in oc(handlerList)) {
				// determine context
				var context = context || handlerMapKey;
				// check settings to see if handler should be called
				var eventLv = ev.getEventLv(context);				
				var levelNameKey = this.settings[handlerMapKey];
				var lv = ev.getEH_lv(levelNameKey);
				if (eventLv <= lv) {
					var lvName = ev.getEventLvName(handlerName);
					var handler = evh[lvName][handlerName];
					if (handler) {
						handler(event);
					} else log('WARNING: No handler found in context "' + context + '" for ' + handlerName + ' <WARNING>')					
				}
				return;
			}
		}
		log('WARNING: Eventhandler for "' + handlerName + '" not found <WARNING>');
		*/
	}
}
